package org.sauceggplant;

import javax.swing.*;
import java.util.concurrent.CountDownLatch;

public class Progress implements Runnable {
    JProgressBar progressBar;
    private int size;
    private CountDownLatch countDownLatch;

    public Progress(JProgressBar progressBar, int size, CountDownLatch countDownLatch) {
        this.progressBar = progressBar;
        this.size = size;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        while (countDownLatch.getCount() > 0) {
            System.out.println("Progress block size: " + (size - countDownLatch.getCount()));
            progressBar.setValue((int) (size - countDownLatch.getCount()));
            progressBar.updateUI();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        progressBar.setValue(size);
        progressBar.updateUI();
    }
}